import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

class AthleteLoginEvent extends JFrame implements ItemListener, ActionListener {
    // 声明类的成员属性
    private JLabel lbNo, lbSports, lbRace;
    private JTextField tfNo;
    private JRadioButton rbNan, rbNv;
    private ButtonGroup grp;
    private JComboBox<String> jcSports;
    private JTextArea jtRace;
    private JScrollPane jsRace;
    private JButton btnReset, btnExit;
    private JPanel jp1, jp2, jp3, jp4, jp5;

    public AthleteLoginEvent(String title) {
        super(title);
        // 设置窗体大小
        setPreferredSize(new Dimension(450, 450)); // 首选大小450x450
        setLayout(new GridLayout(5, 1));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // 创建面板、组件对象
        jp1 = new JPanel();
        jp2 = new JPanel();
        jp3 = new JPanel();
        jp4 = new JPanel();
        jp5 = new JPanel();

        tfNo = new JTextField(10);

        lbNo = new JLabel("输入运动员的编号：");
        lbSports = new JLabel("请选择参加的运动项目：");
        lbRace = new JLabel("运动员参赛项目：");
        rbNan = new JRadioButton("男", true);
        rbNv = new JRadioButton("女");
        grp = new ButtonGroup();
        grp.add(rbNan);
        grp.add(rbNv);
        jcSports = new JComboBox<String>();
        jcSports.addItem("跳远");
        jcSports.addItem("600米赛跑");
        jcSports.addItem("跳高");
        jcSports.addItem("4*100接力赛");
        jtRace = new JTextArea(40, 30);
        jsRace = new JScrollPane(jtRace);


        // 设置滚动面板的滚动条为垂直滚动条
        jsRace.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        btnReset = new JButton("重置");
        btnExit = new JButton("退出");

        // 将组件添加到面板
        jp1.add(lbNo);
        jp1.add(tfNo);

        jp2.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "选择性别"));
        jp2.add(rbNan);
        jp2.add(rbNv);

        jp3.add(lbSports);
        jp3.add(jcSports);

        jp4.setLayout(new GridLayout(2, 1));
        jp4.add(lbRace);
        jp4.add(jsRace);

        jp5.add(btnReset);
        jp5.add(btnExit);

        // 将面板添加到窗体
        add(jp1);
        add(jp2);
        add(jp3);
        add(jp4);
        add(jp5);

        // 为事件源添加监听器
        rbNan.addItemListener(this);
        rbNv.addItemListener(this);
        jcSports.addActionListener(this);
        btnReset.addActionListener(this);
        btnExit.addActionListener(this);

        pack();
        setVisible(true);
    }

    // 实现ItemListener接口的方法
    public void itemStateChanged(ItemEvent e) {
        if (e.getStateChange() == e.SELECTED) {
            if (e.getItem() == rbNan) {
                jcSports.removeAllItems();
                jcSports.addItem("铅球");
                jcSports.addItem("男子1000米赛跑");
                jcSports.addItem("男子400米游泳");
                jcSports.addItem("篮球");
            } else if (e.getItem() == rbNv) {
                jcSports.removeAllItems();
                jcSports.addItem("铅球");
                jcSports.addItem("女子600米赛跑");
                jcSports.addItem("女子100米游泳");
                jcSports.addItem("排球");
            }
        }
        jtRace.setText("");
    }

    // 实现ActionListener接口的方法
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == jcSports) {
            lbRace.setText(tfNo.getText() + "运动员参赛项目：");
            jtRace.setEditable(true);
            jtRace.append(jcSports.getSelectedItem() + "\n");
            jtRace.setEditable(false);
        } else if (e.getSource() == btnReset) {
            tfNo.setText("");
            rbNan.setSelected(true);
            jcSports.setSelectedIndex(0);
            lbRace.setText("运动员参赛项目：");
            jtRace.setEditable(true);
            jtRace.setText("");
            jtRace.setEditable(false);
        } else if (e.getSource() == btnExit) {
            System.exit(0);
        }
    }

    public static void main(String[] args) {
        new AthleteLoginEvent("运动员登录");
    }
}